package com.cskaoyan.generator;

import com.cskaoyan.bean.LiuShifu;
import net.sf.cglib.core.DebuggingClassWriter;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.InvocationHandler;

import java.lang.reflect.Method;

public class CglibGenerator {

    public static void main(String[] args) {
        //保存cglib字节码文件
        System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY,
                "D:\\WorkSpace\\j33_workspace\\codes\\day02-design-pattern\\demo5-cglib-dynamic-proxy");

        LiuShifu liuShifu = new LiuShifu();
        //生成代理对象
        //代理对象可以使用委托类来接收，代理类继承了委托类
        /*LiuShifu proxy = (LiuShifu) Enhancer.create(LiuShifu.class, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] objects) throws Throwable {
                Object invoke = method.invoke(liuShifu, objects);
                System.out.println("五根烤鸡翅");
                return invoke;
            }
        });*/
        LiuShifu proxy = (LiuShifu) Enhancer.create(LiuShifu.class, new CustomInvocationHandler());
        proxy.buyLittleNight("火锅","啤酒");
    }
}
